package com.mvp.shq.washer.view.widget.addressPicker;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mvp.shq.washer.R;
import com.mvp.shq.washer.entity.shop.address.AreaData;
import com.mvp.shq.washer.entity.shop.address.CityData;
import com.mvp.shq.washer.entity.shop.address.ProvinceData;
import com.mvp.shq.washer.entity.shop.address.UnitData;
import com.mvp.shq.washer.utils.T;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/10/10.
 */

public class AddressPickerAdapter extends RecyclerView.Adapter<AddressPickerAdapter.ViewHolder> {

    private Context context;
    //显示内容 0 省 1 市  2 区 3 单位
    private int TYPE = 0;
    private List<ProvinceData> provinces;
    private List<CityData> citys;
    private List<AreaData> areas;
    private List<UnitData> units;
    private OnItemClickListener listener;

    public AddressPickerAdapter(Context mContext,int type) {
        context = mContext;
        TYPE = type;
    }

    public AddressPickerAdapter(Context mContext,List<ProvinceData> list) {
        context = mContext;
        TYPE = 0;
        provinces = list;
    }
    public AddressPickerAdapter(Context mContext,ProvinceData provinceData) {
        context = mContext;
        TYPE = 1;
        citys = provinceData.getCitys();
    }
    public AddressPickerAdapter(Context mContext,CityData cityData) {
        context = mContext;
        TYPE = 2;
        areas = cityData.getAreas();
    }
    public AddressPickerAdapter(Context mContext,AreaData areaData) {
        context = mContext;
        TYPE = 3;
        units = areaData.getUnits();
    }

    public void setListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public void setProvinces(List<ProvinceData> provinces) {
        this.provinces = provinces;
    }

    public void setCitys(List<CityData> citys) {
        this.citys = citys;
    }

    public void setAreas(List<AreaData> areas) {
        this.areas = areas;
    }

    public void setUnits(List<UnitData> units) {
        this.units = units;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_address_picker, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        boolean isSelect = false;
        switch (TYPE){
            case 0:
                //省
                isSelect = provinces.get(position).isSelect();
                holder.tvName.setText(provinces.get(position).getName());
                break;
            case 1:
                //市
                isSelect = citys.get(position).isSelect();
                holder.tvName.setText(citys.get(position).getName());
                break;
            case 2:
                //区
                isSelect = areas.get(position).isSelect();
                holder.tvName.setText(areas.get(position).getName());
                break;
            case 3:
                //单位
                isSelect = units.get(position).isSelect();
                holder.tvName.setText(units.get(position).getName());
                break;
        }
        if (isSelect){
            holder.imgSelect.setImageResource(R.mipmap.cart_selected);
        }else {
            holder.imgSelect.setImageResource(R.mipmap.cart_pressed);
        }
        holder.rlContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onItemClick(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        switch (TYPE){
            case 0:
                return provinces.size();
            case 1:
                return citys.size();
            case 2:
                return areas.size();
            case 3:
                return units.size();
        }
        return 0;
    }



    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.img_select)
        ImageView imgSelect;
        @BindView(R.id.rl_container)
        RelativeLayout rlContainer;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
    public interface OnItemClickListener{
        void onItemClick(int position);
    }
}
